package com.systemic.service.impl;

import com.systemic.mapper.GroupJoinApplyMapper;
import com.systemic.mapper.GroupRelationMapper;
import com.systemic.pojo.GroupJoinApply;
import com.systemic.pojo.GroupRelation;
import com.systemic.service.GroupJoinApplyService;
import com.systemic.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: GroupJoinApplyServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 10:50
 * @Version:
 **/
@Service
public class GroupJoinApplyServiceImpl implements GroupJoinApplyService {

    @Resource
    private GroupJoinApplyMapper groupJoinApplyMapper;

    @Resource
    private GroupRelationMapper groupRelationMapper;

    /**
     * 加入申请
     * @param groupJoinApply
     * @return
     */
    @Override
    @Transactional
    public int joinApply(GroupJoinApply groupJoinApply) {
        int row = 0;
        //查询用户是否已经加入或申请
        List<GroupJoinApply> res1 = groupJoinApplyMapper.select(groupJoinApply);
        if (!res1.isEmpty()){
            throw new RuntimeException("用户已经申请");
        }
        GroupRelation groupRelation = new GroupRelation();
        groupRelation.setUid(groupJoinApply.getUid());
        groupRelation.setGroupId(groupJoinApply.getGroupId());
        List<GroupRelation> res2 = groupRelationMapper.select(groupRelation);
        if (!res2.isEmpty()){
            throw new RuntimeException("用户已经申请");
        }
        groupJoinApply.setGmtCreate(new Date());
        row = groupJoinApplyMapper.insert(groupJoinApply)+row;
        return row;
    }

    /**
     * 查看加入申请
     * @param pageVo
     * @return
     */
    @Override
    public List<GroupJoinApply> selectListById(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        Integer groupId = pageVo.getId();
        return groupJoinApplyMapper.selectListById(groupId,offset,pageSize);
    }

    /**
     * 同意加入申请
     * @param groupJoinApply
     * @return
     */
    @Override
    @Transactional
    public int joinAgree(GroupJoinApply groupJoinApply) {
        int row = 0;
        GroupRelation groupRelation = new GroupRelation();
        groupRelation.setGroupId(groupJoinApply.getGroupId());
        groupRelation.setUid(groupJoinApply.getUid());
        groupRelation.setJoinTime(new Date());
        groupRelation.setStatus(3);   // status = 3 表示成员
        List<GroupRelation> res2 = groupRelationMapper.select(groupRelation);
        if (!res2.isEmpty()){
            throw new RuntimeException("用户已经申请");
        }
        // 新增团体关系
        row = groupRelationMapper.insert(groupRelation)+row;
        row = groupJoinApplyMapper.delete(groupJoinApply)+row;
        return row;
    }

    /**
     * 忽略加入申请
     * @param groupJoinApply
     * @return
     */
    @Override
    @Transactional
    public int joinDisagree(GroupJoinApply groupJoinApply) {
        int row = 0;
        row = groupJoinApplyMapper.delete(groupJoinApply)+row;
        return row;
    }
}
